/**
 * PM2 生态系统配置文件
 * PM2 Ecosystem Configuration File
 * 
 * 此文件定义了个人云笔记应用的PM2进程管理配置
 * This file defines the PM2 process management configuration for Personal Cloud Notes
 */

module.exports = {
  apps: [
    {
      // 应用基本信息 / Application Basic Information
      name: 'personal-cloud-notes',
      script: './server/app.js',
      cwd: '/var/www/personal-cloud-notes',

      // 进程管理配置 / Process Management Configuration
      instances: 'max', // 使用所有CPU核心 / Use all CPU cores
      exec_mode: 'cluster', // 集群模式 / Cluster mode

      // 环境变量 / Environment Variables
      env: {
        NODE_ENV: 'production',
        PORT: 3000,

        // 数据库配置 / Database Configuration
        DB_HOST: 'localhost',
        DB_PORT: 3306,
        DB_NAME: 'personal_cloud_notes',
        DB_USER: 'notes_user',
        DB_PASSWORD: 'your_secure_password',
        DB_CONNECTION_LIMIT: 10,

        // JWT配置 / JWT Configuration
        JWT_SECRET: 'your_super_secret_jwt_key_here_change_in_production',
        JWT_EXPIRES_IN: '7d',

        // 文件上传配置 / File Upload Configuration
        UPLOAD_PATH: '/var/www/personal-cloud-notes/data/users',
        MAX_FILE_SIZE: 104857600, // 100MB
        MAX_IMAGE_SIZE: 5242880,  // 5MB
        MAX_VIDEO_SIZE: 52428800, // 50MB
        MAX_AUDIO_SIZE: 10485760, // 10MB

        // 存储配置 / Storage Configuration
        DEFAULT_STORAGE_LIMIT: 32212254720, // 30GB

        // 安全配置 / Security Configuration
        BCRYPT_ROUNDS: 10,
        RATE_LIMIT_WINDOW_MS: 900000, // 15分钟
        RATE_LIMIT_MAX_REQUESTS: 100,

        // 备份配置 / Backup Configuration
        BACKUP_PATH: '/var/www/personal-cloud-notes/data/backups',
        BACKUP_RETENTION_DAYS: 7,

        // 日志配置 / Logging Configuration
        LOG_LEVEL: 'info',
        LOG_PATH: '/var/www/personal-cloud-notes/data/logs',
        LOG_MAX_SIZE: '10m',
        LOG_MAX_FILES: '7d',
        LOG_ENABLE_CONSOLE: 'false',
        LOG_ENABLE_FILE: 'true',

        // 性能监控配置 / Performance Monitoring Configuration
        ENABLE_PERFORMANCE_MONITORING: 'true',
        METRICS_INTERVAL: 30000,
        MEMORY_ALERT_THRESHOLD: 80,
        CPU_ALERT_THRESHOLD: 80,
        RESPONSE_TIME_ALERT_THRESHOLD: 5000,
        STORAGE_ALERT_THRESHOLD: 90,

        // 缓存配置 / Cache Configuration
        ENABLE_STATIC_CACHE: 'true',
        STATIC_CACHE_MAX_AGE: 86400,
        ENABLE_GZIP: 'true',
        GZIP_LEVEL: 6
      },

      // 开发环境配置 / Development Environment Configuration
      env_development: {
        NODE_ENV: 'development',
        PORT: 3000,
        DB_HOST: 'localhost',
        DB_NAME: 'personal_cloud_notes_dev',
        LOG_LEVEL: 'debug',
        LOG_ENABLE_CONSOLE: 'true',
        ENABLE_PERFORMANCE_MONITORING: 'false'
      },

      // 测试环境配置 / Test Environment Configuration
      env_test: {
        NODE_ENV: 'test',
        PORT: 3001,
        DB_HOST: 'localhost',
        DB_NAME: 'personal_cloud_notes_test',
        LOG_LEVEL: 'error',
        LOG_ENABLE_CONSOLE: 'false',
        ENABLE_PERFORMANCE_MONITORING: 'false'
      },

      // 预发布环境配置 / Staging Environment Configuration
      env_staging: {
        NODE_ENV: 'staging',
        PORT: 3000,
        DB_NAME: 'personal_cloud_notes_staging',
        LOG_LEVEL: 'warn',
        ENABLE_PERFORMANCE_MONITORING: 'true'
      },

      // 进程重启配置 / Process Restart Configuration
      autorestart: true,
      watch: false, // 生产环境不启用文件监控 / Disable file watching in production
      max_memory_restart: '1G', // 内存超过1GB时重启 / Restart when memory exceeds 1GB
      restart_delay: 4000, // 重启延迟4秒 / Restart delay 4 seconds
      max_restarts: 10, // 最大重启次数 / Maximum restart attempts
      min_uptime: '10s', // 最小运行时间 / Minimum uptime

      // 日志配置 / Logging Configuration
      log_file: '/var/www/personal-cloud-notes/data/logs/pm2-combined.log',
      out_file: '/var/www/personal-cloud-notes/data/logs/pm2-out.log',
      error_file: '/var/www/personal-cloud-notes/data/logs/pm2-error.log',
      log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
      merge_logs: true,

      // 进程配置 / Process Configuration
      kill_timeout: 5000, // 强制杀死进程的超时时间 / Timeout for force kill
      listen_timeout: 3000, // 监听超时时间 / Listen timeout

      // 健康检查配置 / Health Check Configuration
      health_check_grace_period: 3000,

      // 高级配置 / Advanced Configuration
      node_args: [
        '--max-old-space-size=1024', // 限制V8堆内存为1GB / Limit V8 heap memory to 1GB
        '--optimize-for-size' // 优化内存使用 / Optimize for memory usage
      ],

      // 源码映射支持 / Source Map Support
      source_map_support: true,

      // 进程间通信 / Inter-Process Communication
      pmx: true,

      // 自动化部署配置 / Automated Deployment Configuration
      post_update: ['npm install', 'npm run build'], // 更新后执行的命令 / Commands to run after update

      // 监控配置 / Monitoring Configuration
      monitoring: true,

      // 实例变量 / Instance Variables
      instance_var: 'INSTANCE_ID',

      // 错误处理 / Error Handling
      ignore_watch: [
        'node_modules',
        'data/logs',
        'data/backups',
        'data/temp',
        'data/users'
      ],

      // 时间配置 / Time Configuration
      time: true,

      // 合并日志 / Merge Logs
      combine_logs: true,

      // 进程标题 / Process Title
      treekill: true
    }
  ],

  // 部署配置 / Deployment Configuration
  deploy: {
    // 生产环境部署 / Production Deployment
    production: {
      user: 'deploy',
      host: ['your-server.com'],
      ref: 'origin/main',
      repo: 'https://github.com/your-username/personal-cloud-notes.git',
      path: '/var/www/personal-cloud-notes',
      'pre-deploy-local': '',
      'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env production',
      'pre-setup': '',
      'ssh_options': 'StrictHostKeyChecking=no'
    },

    // 预发布环境部署 / Staging Deployment
    staging: {
      user: 'deploy',
      host: ['staging-server.com'],
      ref: 'origin/develop',
      repo: 'https://github.com/your-username/personal-cloud-notes.git',
      path: '/var/www/personal-cloud-notes-staging',
      'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env staging',
      'ssh_options': 'StrictHostKeyChecking=no'
    }
  }
};